package com.kmdd.xtds.ui

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.kmdd.xtds.R
import com.kmdd.xtds.configs.PrefsManager
import com.kmdd.xtds.databinding.FragmentHealthyBinding
import com.kmdd.xtds.room.BloodPressure
import com.kmdd.xtds.room.BloodPressureDatabase
import com.kmdd.xtds.util.*
import com.haibin.calendarview.Calendar
import com.haibin.calendarview.CalendarView
import kotlinx.coroutines.*
import kotlinx.coroutines.launch

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class HealthyFragment : Fragment() {

    private var _binding: FragmentHealthyBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    var date = ""

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentHealthyBinding.inflate(inflater, container, false)
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.calendarView.setWeeColor(
            resources.getColor(R.color.transparent),
            resources.getColor(R.color.color_6C6C6C)
        )
        initHeartChartView(
            binding.barchartRate
        )

        binding.calendarView.setOnCalendarSelectListener(object :
            CalendarView.OnCalendarSelectListener {
            override fun onCalendarOutOfRange(calendar: Calendar?) {

            }

            override fun onCalendarSelect(calendar: Calendar, isClick: Boolean) {
                binding.tvTime.text =
                    TimeUtil.getTime(calendar.timeInMillis, TimeUtil.DATEFORMATER2)
                selectDao(TimeUtil.getTime(calendar.timeInMillis, TimeUtil.DATEFORMATER2))
            }

        })

        binding.ivAdd.setOnClickListener {
            startActivity(Intent(requireContext(), BloodOxygenAddActivity::class.java))
        }
        LiveDataBus.get().with("refreshBloodOxygenData").observe(viewLifecycleOwner) {
            initData()
            selectDao(date)
        }

        binding.tvTime.text =
            TimeUtil.getTime(System.currentTimeMillis(), TimeUtil.DATEFORMATER9)
        initData()


        binding.calendarView.scrollToCurrent()
    }

    fun initData() {
        binding.tvShrink.text = PrefsManager.getShrink()
        binding.tvBloodOxygenSaturation.text = "${PrefsManager.getBloodOxygenSaturation()}%"
        binding.tvDiastolicBloodPressure.text = PrefsManager.getDiastolicBloodPressure()

        val shrink = PrefsManager.getShrink().toInt()
        val diastolicBloodPressure = PrefsManager.getDiastolicBloodPressure().toInt()
        if (shrink in 91..138 && diastolicBloodPressure in 60..89) {
            binding.tvState.text = "状态：正常"
            binding.tvState.setTextColor(resources.getColor(R.color.color_00C7BE))
        } else {
            binding.tvState.text = "状态：异常"
            binding.tvState.setTextColor(resources.getColor(R.color.color_red))
        }
    }

    fun selectDao(date: String) {
        this.date = date
        launch {
            var list: List<BloodPressure>? = null
            withContext(Dispatchers.IO) {
                list = BloodPressureDatabase.getInstance(context!!)
                    ?.bloodPressureDao()
                    ?.getBloodByTime(date)
            }
            if (list != null && !list.isNullOrEmpty()) {
                setHeartChartData(binding.barchartRate, list!!, 100)
            } else {
                binding.barchartRate.clear()
            }
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}